home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 January - Disc 2 / Macworld (1999-01) (Disk 2).dmg / Shareware World / Utilities / User Interface / FinderPop175 / FinderPoplets / Source&API / SwitchToFinder.cp < prev   
Encoding:
Text File  |  1998-10-18  |  2.9 KB  |  97 lines  |  [TEXT/CWIE]

  1. //=====================================================================================
  2. // SwitchFinder.cp
  3. //
  4. // A FinderPoplet to switch to the Finder and hide others.
  5. // Does this by furtling with the process menu.  Nasty.  Yeah.
  6. //
  7. //
  8. //    Written by:    turly o’connor, cork, ireland        turly@geocities.com
  9. //
  10. //    Copyright:    1997, 1998 by Turlough O'Connor, all rights reserved.
  11. //
  12. //    Change History (most recent first):
  13. //        <01>    03-Oct-98    tur        Basic Version
  14. //
  15. //=====================================================================================
  16.  
  17. #include <MacTypes.h>
  18. #include "FinderPopAPI.h"
  19.  
  20. enum {kFinderType = 'FNDR', kFinderCreator = 'MACS'};        // Finder file info
  21.  
  22. static OSErr FindProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumber *processSN);
  23.  
  24. //
  25. // if we're being called for the first time, detach ourselves in case the host app quits
  26. // and our Fplt file ends up being closed underneath us!
  27. //
  28.  
  29. pascal UInt32 main(TFinderPopletInfo *info)
  30. {
  31.     UInt32    retVal = (info->fpltRefCon != 0) ? kFinderPopletContinue : kFinderPopletDetachAndStayResident;
  32.  
  33.     if (1)        // (info->fpltEvent != NULL && info->fpltEvent->what == nullEvent)
  34.     {
  35.         ProcessSerialNumber    finderPSN, frontPSN;
  36.         Boolean                sameProcess;
  37.  
  38.         info->fpltRefCon++;                                    // #times we were called...
  39.         sameProcess = false;
  40.  
  41.         if (GetFrontProcess(&frontPSN) == noErr)
  42.         {
  43.             if (FindProcess(kFinderType, kFinderCreator, &finderPSN) == noErr)
  44.             {
  45.                 if (SameProcess(&frontPSN, &finderPSN, &sameProcess) == noErr)
  46.                 {
  47.                     if (sameProcess)                        // Finder is already frontmost
  48.                     {                                        // so furtle with SysMenu to "Hide Others"
  49.                         SystemMenu(0xBF970002);                // System Menu, Item #2  -- yuck!
  50.                         retVal = kFinderPopletTerminate;
  51.                     }
  52.                     else                                    // try bringing the Finder frontmost
  53.                         SetFrontProcess(&finderPSN);
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59.     if (info->fpltRefCon > 30)                                // called more than 30 times? Abort.
  60.     {                                                        // There's probably a dialog up or something...
  61.         SysBeep(0);
  62.         retVal = kFinderPopletTerminate;
  63.     }
  64.  
  65.     return retVal;
  66. }
  67.  
  68. //===================================================================================
  69. // FindProcess
  70. //
  71. // Search through the current process list to find the given application.
  72. // 
  73. //===================================================================================
  74.  
  75. static OSErr FindProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumber *processSN)
  76. {
  77.     ProcessInfoRec    tempInfo;
  78.     OSErr            err = noErr;
  79.                                                         // start at the beginning of the process list
  80.     processSN->lowLongOfPSN = kNoProcess;
  81.     processSN->highLongOfPSN = kNoProcess;
  82.  
  83.     tempInfo.processInfoLength = sizeof(ProcessInfoRec);
  84.     tempInfo.processName = NULL;
  85.     tempInfo.processAppSpec = NULL;
  86.  
  87.     do
  88.     {
  89.         err = GetNextProcess(processSN);
  90.         if (err == noErr)
  91.             err = GetProcessInformation(processSN, &tempInfo);
  92.     }
  93.     while (err == noErr && (tempInfo.processSignature != creatorToFind || tempInfo.processType != typeToFind));
  94.  
  95.     return err;
  96. }                                                        // FindProcess
  97.